import service from "@/util/request";
import { ElMessage } from "element-plus";
const config: any = {
  /**
   * api请求基础路径
   */
  base_url: import.meta.env.VITE_BASE_URL,
  /**
   * 接口成功返回状态码
   */
  result_code: 200,

  /**
   * 接口请求超时时间
   */
  request_timeout: 30000,

  /**
   * 默认接口请求类型
   * 可选值：application/x-www-form-urlencoded multipart/form-data
   */
  default_headers: "application/json",
};

const { default_headers } = config;

const request = (option: any) => {
  const {
    url,
    method,
    params,
    data,
    headersType,
    responseType,
    baseApiUrl = import.meta.env.VITE_API_URL,
    timeout,
  } = option;
  return service({
    url: baseApiUrl + url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      "Content-Type": headersType || default_headers,
    },
    timeout,
  });
};
export default {
  get: async <T = any>(option: any) => {
    const successMsg =
      option.data.successMsg != undefined ? option.data.successMsg : false;
    const errorMsg =
      option.data.errorMsg != undefined ? option.data.errorMsg : false;
    delete option.data.successMsg;
    delete option.data.errorMsg;
    try {
      const res: any = await request({
        method: "GET",
        ...option,
      });
      successMsg ? ElMessage.success(res.msg) : "";
      return res.data as unknown as T;
    } catch (error: any) {
      errorMsg ? ElMessage.error(JSON.parse(error).data.msg) : "";
      throw ""; //终止代码
    }
  },
  post: async <T = any>(option: any) => {
    const successMsg =
      option.data.successMsg != undefined ? option.data.successMsg : false;
    const errorMsg =
      option.data.errorMsg != undefined ? option.data.errorMsg : false;
    delete option.data.successMsg;
    delete option.data.errorMsg;
    if (JSON.stringify(option.data) == "{}") {
      option.data = undefined;
    }
    try {
      const res: any = await request({ method: "POST", ...option });
      successMsg ? ElMessage.success(res.msg) : "";
      console.log("post_result:", res);
      return res.data as unknown as T;
    } catch (error: any) {
      errorMsg ? ElMessage.error(JSON.parse(error).msg) : "";
      throw ""; //终止代码
    }
  },
  postOriginal: async (option: any) => {
    const successMsg =
      option.data.successMsg != undefined ? option.data.successMsg : false;
    const errorMsg =
      option.data.errorMsg != undefined ? option.data.errorMsg : false;
    delete option.data.successMsg;
    delete option.data.errorMsg;
    try {
      const res: any = await request({ method: "POST", ...option });
      successMsg ? ElMessage.success(res.msg) : "";
      return res;
    } catch (error: any) {
      errorMsg ? ElMessage.error(JSON.parse(error).msg) : "";
      throw "";
    }
  },
  delete: async <T = any>(option: any) => {
    const successMsg =
      option.data.successMsg != undefined ? option.data.successMsg : false;
    const errorMsg =
      option.data.errorMsg != undefined ? option.data.errorMsg : false;
    delete option.data.successMsg;
    delete option.data.errorMsg;
    try {
      const res: any = await request({ method: "DELETE", ...option });
      successMsg ? ElMessage.success(res.msg) : "";
      return res.data as unknown as T;
    } catch (error: any) {
      errorMsg ? ElMessage.error(JSON.parse(error).msg) : "";
      throw "";
    }
  },
  put: async <T = any>(option: any) => {
    const successMsg =
      option.data.successMsg != undefined ? option.data.successMsg : false;
    const errorMsg =
      option.data.errorMsg != undefined ? option.data.errorMsg : false;
    delete option.data.successMsg;
    delete option.data.errorMsg;
    try {
      const res: any = await request({ method: "PUT", ...option });
      successMsg ? ElMessage.success(res.msg) : "";
      return res.data as unknown as T;
    } catch (error: any) {
      errorMsg ? ElMessage.error(JSON.parse(error).msg) : "";
      throw "";
    }
  },
  download: async <T = any>(option: any) => {
    const successMsg =
      option.data.successMsg != undefined ? option.data.successMsg : false;
    const errorMsg =
      option.data.errorMsg != undefined ? option.data.errorMsg : false;
    delete option.data.successMsg;
    delete option.data.errorMsg;
    try {
      const res: any = await request({
        method: "GET",
        responseType: "blob",
        ...option,
      });
      successMsg ? ElMessage.success(res.msg) : "";
      return res.data as unknown as Promise<T>;
    } catch (error: any) {
      errorMsg ? ElMessage.error(JSON.parse(error).msg) : "";
      throw "";
    }
  },
  upload: async <T = any>(option: any) => {
    const successMsg =
      option.data.successMsg != undefined ? option.data.successMsg : false;
    const errorMsg =
      option.data.errorMsg != undefined ? option.data.errorMsg : false;
    delete option.data.successMsg;
    delete option.data.errorMsg;
    try {
      option.data.headersType = "multipart/form-data";
      const res: any = await request({ method: "POST", ...option });
      successMsg ? ElMessage.success(res.msg) : "";
      return res.data as unknown as Promise<T>;
    } catch (error: any) {
      errorMsg ? ElMessage.error(JSON.parse(error).msg) : "";
      throw "";
    }
  },
};
